﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Library.Data.Xml;

namespace UnigenReport
{
    public static class SystemConfig
    {
        /// <summary>
        /// Ten tep cau hinh he thong
        /// </summary>
        private static string _filepath = "SystemConfigData.xml";

        /// <summary>
        /// Noi dung tep cau hinh
        /// </summary>
        private static XmlDocument Document
        {
            get { return Nested.instance; }
        }

        internal class Nested
        {
            static Nested()
            {
            }
            internal static readonly XmlDocument instance = new XmlDocument();
        }

        static SystemConfig()
        {

        }

        public static void Load()
        {
            try
            {
                Document.Load(_filepath);
            }
            catch { /**/}
        }

        public static void Load(string filePath)
        {
            try
            {
                Document.Load((_filepath = filePath));
            }
            catch { /**/}
        }

        /// <summary>
        /// Luu thong tin cau hinh
        /// </summary>
        public static void Save()
        {
            try
            {
                Document.Save(_filepath);
            }
            catch { /**/}
        }

        public static void Save(string filePath)
        {
            try
            {
                Document.Save(_filepath = filePath);
            }
            catch { /**/}
        }

        #region Login

        /// <summary>
        /// Thong tin dang nhap 
        /// </summary>
        public class Login
        {
            /// <summary>
            /// Get/Set Remember
            /// </summary>
            public bool IsRemember
            {
                get
                {
                    bool isOK = false;

                    var node = Document.SelectSingleNode("/root/login");
                    var value = "";
                    XmlHelper.GetAttributeValue(node, "isRemember", ref value);

                    bool.TryParse(value, out isOK);
                    return isOK;
                }
                set
                {
                    var node = Document.SelectSingleNode("/root/login");
                    XmlHelper.SetAttributeValue(node, "isRemember", value.ToString());
                }
            }

            /// <summary>
            /// Get/Set User Info
            /// </summary>
            public UserInfo User
            {
                get
                {
                    var username = "";
                    var password = "";
                    var node = Document.SelectSingleNode("/root/login/user");

                    XmlHelper.GetItemValue(node, "username", ref username);
                    XmlHelper.GetItemValue(node, "password", ref password);

                    return new UserInfo() { UserName = username, Password = password };
                }
                set
                {
                    var info = value as UserInfo;
                    var node = Document.SelectSingleNode("/root/login/user");

                    XmlHelper.SetItemValue(node, "username", info.UserName);
                    XmlHelper.SetItemValue(node, "password", info.Password);
                }
            }

            /// <summary>
            /// Thong tin NSD
            /// </summary>
            public class UserInfo
            {
                public string UserName
                {
                    get;
                    set;
                }

                public string Password
                {
                    get;
                    set;
                }
            }
        }

        #endregion

        #region Company

        public class Company
        {
            /// <summary>
            /// Name
            /// </summary>
            public string Name
            {
                get
                {
                    var name = "";
                    var node = Document.SelectSingleNode("/root/company");

                    XmlHelper.GetItemValue(node, "name", ref name);
                    return name;
                }
                set
                {
                    var name = value.ToString();
                    var node = Document.SelectSingleNode("/root/company");

                    XmlHelper.SetItemValue(node, "name", name);
                }
            }

            /// <summary>
            /// Address
            /// </summary>
            public string Address
            {
                get
                {
                    var address = "";
                    var node = Document.SelectSingleNode("/root/company");

                    XmlHelper.GetItemValue(node, "address", ref address);
                    return address;
                }
                set
                {
                    var address = value.ToString();
                    var node = Document.SelectSingleNode("/root/company");

                    XmlHelper.SetItemValue(node, "address", address);
                }
            }

            /// <summary>
            /// Province
            /// </summary>
            public string Province
            {
                get
                {
                    var province = "";
                    var node = Document.SelectSingleNode("/root/company");

                    XmlHelper.GetItemValue(node, "province", ref province);
                    return province;
                }
                set
                {
                    var province = value.ToString();
                    var node = Document.SelectSingleNode("/root/company");

                    XmlHelper.SetItemValue(node, "province", province);
                }
            }

            /// <summary>
            /// Phone
            /// </summary>
            public string Phone
            {
                get
                {
                    var phone = "";
                    var node = Document.SelectSingleNode("/root/company");

                    XmlHelper.GetItemValue(node, "phone", ref phone);
                    return phone;
                }
                set
                {
                    var phone = value.ToString();
                    var node = Document.SelectSingleNode("/root/company");

                    XmlHelper.SetItemValue(node, "phone", phone);
                }
            }

            /// <summary>
            /// Fax
            /// </summary>
            public string Fax
            {
                get
                {
                    var fax = "";
                    var node = Document.SelectSingleNode("/root/company");

                    XmlHelper.GetItemValue(node, "fax", ref fax);
                    return fax;
                }
                set
                {
                    var fax = value.ToString();
                    var node = Document.SelectSingleNode("/root/company");

                    XmlHelper.SetItemValue(node, "fax", fax);
                }
            }
        }

        #endregion

        #region User config

        public class UserConfig
        {
            /// <summary>
            /// Printer name
            /// </summary>
            public string PrinterName
            {
                get
                {
                    var printername = "";
                    var node = Document.SelectSingleNode("/root/userconfig");

                    XmlHelper.GetItemValue(node, "printername", ref printername);
                    return printername;
                }
                set
                {
                    var printername = value.ToString();
                    var node = Document.SelectSingleNode("/root/userconfig");

                    XmlHelper.SetItemValue(node, "printername", printername);
                }
            }

            /// <summary>
            /// PrintCopies
            /// </summary>
            public int PrintCopies
            {
                get
                {
                    var printCopies = "";
                    var node = Document.SelectSingleNode("/root/userconfig");

                    XmlHelper.GetItemValue(node, "printCopies", ref printCopies);
                    return Convert.ToInt32("0" + printCopies);
                }
                set
                {
                    var printCopies = value;
                    var node = Document.SelectSingleNode("/root/userconfig");

                    XmlHelper.SetItemValue(node, "printCopies", printCopies);
                }
            }

            /// <summary>
            /// PaymentTerm
            /// </summary>
            public string PaymentTerm
            {
                get
                {
                    var paymentTerm = "";
                    var node = Document.SelectSingleNode("/root/userconfig");

                    XmlHelper.GetItemValue(node, "paymentTerm", ref paymentTerm);
                    return paymentTerm;
                }
                set
                {
                    var paymentTerm = value.ToString();
                    var node = Document.SelectSingleNode("/root/userconfig");

                    XmlHelper.SetItemValue(node, "paymentTerm", paymentTerm);
                }
            }
        }

        #endregion

        #region Manufacturing

        public class Manufacturing
        {
            /// <summary>
            /// Name
            /// </summary>
            public string Name
            {
                get
                {
                    var name = "";
                    var node = Document.SelectSingleNode("/root/manufacturing");

                    XmlHelper.GetItemValue(node, "name", ref name);
                    return name;
                }
                set
                {
                    var name = value.ToString();
                    var node = Document.SelectSingleNode("/root/manufacturing");

                    XmlHelper.SetItemValue(node, "name", name);
                }
            }

            /// <summary>
            /// Address
            /// </summary>
            public string Address
            {
                get
                {
                    var address = "";
                    var node = Document.SelectSingleNode("/root/manufacturing");

                    XmlHelper.GetItemValue(node, "address", ref address);
                    return address;
                }
                set
                {
                    var address = value.ToString();
                    var node = Document.SelectSingleNode("/root/manufacturing");

                    XmlHelper.SetItemValue(node, "address", address);
                }
            }

            /// <summary>
            /// Country
            /// </summary>
            public string Country
            {
                get
                {
                    var country = "";
                    var node = Document.SelectSingleNode("/root/manufacturing");

                    XmlHelper.GetItemValue(node, "country", ref country);
                    return country;
                }
                set
                {
                    var country = value.ToString();
                    var node = Document.SelectSingleNode("/root/manufacturing");

                    XmlHelper.SetItemValue(node, "country", country);
                }
            }
        }

        #endregion

        #region woImportTemp

        public class WoImportTemp
        {
            /// <summary>
            /// WO
            /// </summary>
            public string WO
            {
                get
                {
                    var WO = "";
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.GetItemValue(node, "WO", ref WO);
                    return WO;
                }
                set
                {
                    var WO = value.ToString();
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.SetItemValue(node, "WO", WO);
                }
            }

            /// <summary>
            /// PO
            /// </summary>
            public string PO
            {
                get
                {
                    var PO = "";
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.GetItemValue(node, "PO", ref PO);
                    return PO;
                }
                set
                {
                    var PO = value.ToString();
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.SetItemValue(node, "PO", PO);
                }
            }

            /// <summary>
            /// Product
            /// </summary>
            public string Product
            {
                get
                {
                    var Product = "";
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.GetItemValue(node, "Product", ref Product);
                    return Product;
                }
                set
                {
                    var Product = value.ToString();
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.SetItemValue(node, "Product", Product);
                }
            }

            /// <summary>
            /// Ordered
            /// </summary>
            public string Ordered
            {
                get
                {
                    var Ordered = "";
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.GetItemValue(node, "Ordered", ref Ordered);
                    return Ordered;
                }
                set
                {
                    var Ordered = value.ToString();
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.SetItemValue(node, "Ordered", Ordered);
                }
            }

            /// <summary>
            /// Shipped
            /// </summary>
            public string Shipped
            {
                get
                {
                    var Shipped = "";
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.GetItemValue(node, "Shipped", ref Shipped);
                    return Shipped;
                }
                set
                {
                    var Shipped = value.ToString();
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.SetItemValue(node, "Shipped", Shipped);
                }
            }

            /// <summary>
            /// Remained
            /// </summary>
            public string Remained
            {
                get
                {
                    var Remained = "";
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.GetItemValue(node, "Remained", ref Remained);
                    return Remained;
                }
                set
                {
                    var Remained = value.ToString();
                    var node = Document.SelectSingleNode("/root/woImportTemp");

                    XmlHelper.SetItemValue(node, "Remained", Remained);
                }
            }
        }

        #endregion
    }
}
